
HAVE_DTRACE=0

# 32 bit
#MARCH=-m32 -msse2 -mfpmath=sse

# 64 bit
MARCH=-m64

OPT=-O2

# comment out this line to enable assertions at runtime
DEFS = -DNDEBUG

# uncommend this line to enable debug compilation
DEBUG ?= #empty by default

CC=gcc -g
CXX=g++ -g
LD=$(CXX) $(OPT) $(MARCH)
WARN=-Wall
CWD=$(shell pwd)
CXXFLAGS=$(OPT) $(MARCH) $(DEBUG) $(INCLUDE) $(WARN) $(DEFS) -fPIC
CCFLAGS=$(CXXFLAGS)



ARCH = $(shell uname)


ifeq ($(HAVE_DTRACE),1)
    DEFS += -DPBRT_PROBES_DTRACE
else
    DEFS += -DPBRT_PROBES_NONE
endif

ifeq ($(ARCH),Linux)
  LIBS += -lpthread
endif
ifeq ($(ARCH),OpenBSD)
  LIBS += -lpthread
endif
ifeq ($(ARCH),Darwin)
  LIBS += -lz
endif


###### Pbrt and Katana directories ######

PBRTTREE?=../pbrt-v2/src

KATANA_HOME?=/home/dev/Apps/Katana

KATANA_PLUGIN_APIS=$(KATANA_HOME)/plugin_apis
KATANA_PLUGIN_SRC=$(KATANA_PLUGIN_APIS)/src


ARGS_FILE_PARSER_HOME=../ArgsFileParser
THIRDPARTY_HOME=../ThirdParty

OPENEXR_HOME=$(THIRDPARTY_HOME)/OpenExr/1.6.1
ZMQ_HOME=$(THIRDPARTY_HOME)/zeromq/3.2.2.rc2
BOOST_HOME=$(THIRDPARTY_HOME)/Boost/1.46.0
EXPAT_HOME=$(THIRDPARTY_HOME)/Expat/2.0.1

######### Render Plugin ##########

RENDER_PLUGIN_OUT=out/Libs/libPbrtPlugin.so

RENDER_PLUGIN_SRC=$(wildcard src/renderPlugin/*.cpp)

# Base Foundry Plugin API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnPluginSystem/FnPlugin.cpp

# Render Plugin API: Base
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/RenderBase.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/CameraSettings.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/DiskRenderOutputProcess.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/GlobalSettings.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/RenderSettings.cpp

# Render Plugin API: Render Actions
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/RenderAction.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/NoOutputRenderAction.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/TemporaryRenderAction.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/CopyRenderAction.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/CopyAndConvertRenderAction.cpp

# Render Plugin API: Interprocess Communication
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/IdSenderFactory.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/SocketConnection.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/Render/SocketIdSender.cpp

# Renderer Info API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RendererInfo/RenderMethod.cpp

# Render Output Utilities API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/RenderOutputUtils.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/ArbitraryOutputAttr.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/CameraInfo.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/ShadingNodeConnectionDescription.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/ShadingNodeDescription.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/ShadingNodeDescriptionMap.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/RenderOutputUtils/XFormMatrix.cpp

# Display Driver API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/ContextSingleton.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/CloseMessage.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/DataMessage.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/KatanaPipe.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/KatanaPipeSingleton.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/Message.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/NewChannelMessage.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/NewFrameMessage.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/ZmqHelpers.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/IsAliveMessage.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnDisplayDriver/EndOfRenderMessage.cpp

# Asset API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnAsset/FnAsset.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnAsset/FnFileSequence.cpp

# Attribute API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnAttribute/FnAttribute.cpp
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnAttribute/FnDataBuilder.cpp

# Logging API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnLogging/FnLogging.cpp

# Scenegraph Iterator API
RENDER_PLUGIN_SRC += $(KATANA_PLUGIN_SRC)/FnScenegraphIterator/FnScenegraphIterator.cpp

RENDER_PLUGIN_OBJS=$(patsubst %.cpp,objs/renderPlugin/%.o,$(RENDER_PLUGIN_SRC))

RENDER_PLUGIN_LIBS=-L$(PBRTTREE)/lib -lpbrt -ldl

RENDER_PLUGIN_INCLUDES=-I./src/renderPlugin
RENDER_PLUGIN_INCLUDES+=-I$(PBRTTREE)/core -I$(PBRTTREE)
RENDER_PLUGIN_INCLUDES+=-I$(KATANA_PLUGIN_APIS)/include

# Third Party libraries
RENDER_PLUGIN_INCLUDES+=-I$(OPENEXR_HOME)/include
RENDER_PLUGIN_INCLUDES+=-I$(ZMQ_HOME)/include
RENDER_PLUGIN_INCLUDES+=-I$(BOOST_HOME)/include
RENDER_PLUGIN_INCLUDES+=-I$(EXPAT_HOME)/include

RENDER_PLUGIN_LIBS += -L$(BOOST_HOME)/lib
RENDER_PLUGIN_LIBS += -L$(ZMQ_HOME)/lib
RENDER_PLUGIN_LIBS += -L$(EXPAT_HOME)/lib

RENDER_PLUGIN_LIBS += -Wl,-Bstatic -lzmq_static -lboost_thread -lexpat
RENDER_PLUGIN_LIBS += -Wl,-Bdynamic

RENDER_PLUGIN_HEADERS=$(wildcard src/renderPlugin/*.h)


######### RenderInfo ##########

RENDER_INFO_OUT=out/Libs/pbrt-renderinfo.so

RENDER_INFO_SRC=$(wildcard src/renderinfo/*.cpp)

# Base Foundry Plugin API
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/FnPluginSystem/FnPlugin.cpp

# Renderer Info API
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/RendererInfo/RendererInfoBase.cpp
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/RendererInfo/LiveRenderControlModule.cpp
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/RendererInfo/LiveRenderFilter.cpp
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/RendererInfo/RenderMethod.cpp
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/RendererInfo/ShaderInfoCache.cpp

# Asset API
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/FnAsset/FnAsset.cpp
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/FnAsset/FnFileSequence.cpp

# Attribute API
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/FnAttribute/FnAttribute.cpp
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/FnAttribute/FnDataBuilder.cpp

# Logging API
RENDER_INFO_SRC += $(KATANA_PLUGIN_SRC)/FnLogging/FnLogging.cpp

RENDER_INFO_OBJS=$(patsubst %.cpp,objs/renderinfo/%.o,$(RENDER_INFO_SRC))

RENDER_INFO_INCLUDES=-I./src/renderinfo
RENDER_INFO_INCLUDES+=-I$(KATANA_PLUGIN_APIS)/include
RENDER_INFO_INCLUDES+=-I$(ARGS_FILE_PARSER_HOME)/include
RENDER_INFO_INCLUDES+=-I$(THIRDPARTY_HOME)/rapidxml/1.13

RENDER_INFO_LIBS=-L$(ARGS_FILE_PARSER_HOME)/out -lArgsFileParser

RENDER_INFO_HEADERS=$(wildcard src/renderinfo/*.h)

###### Targets #########

all: renderPlugin renderinfo
	@echo Done

dirs:
	@mkdir -p out/
	@mkdir -p objs
	#@rm -f $(CWD)/out/Plugins
	#@ln -sf $(CWD)/python/Plugins $(CWD)/out
	@rm -f $(CWD)/out/GenericAssign
	@ln -sf $(CWD)/python/GenericAssign $(CWD)/out
	@rm -f $(CWD)/out/Shaders
	@ln -sf $(CWD)/shaders $(CWD)/out/Shaders
	
renderPlugin: dirs $(RENDER_PLUGIN_OUT)


renderinfo: dirs $(RENDER_INFO_OUT)


$(RENDER_PLUGIN_OBJS):$(RENDER_PLUGIN_HEADERS)


$(RENDER_INFO_OBJS):$(RENDER_INFO_HEADERS)


$(RENDER_PLUGIN_OUT): $(RENDER_PLUGIN_OBJS)
	@echo Building Render Plugin
	@mkdir -p `dirname $@`
	$(CXX) $(CXXFLAGS) $(RENDER_PLUGIN_INCLUDES) $(RENDER_PLUGIN_OBJS) $(RENDER_PLUGIN_LIBS) -shared -o $@


$(RENDER_INFO_OUT): $(RENDER_INFO_OBJS)
	@echo Building RenderInfo
	@mkdir -p `dirname $@`
	$(CXX) $(CXXFLAGS) $(RENDER_INFO_INCLUDES) $(RENDER_INFO_OBJS) $(RENDER_INFO_LIBS) -shared -o $@


objs/renderPlugin/%.o: %.cpp
	@echo " Building object $@"
	@mkdir -p `dirname $@`
	$(CXX) $(CXXFLAGS) $(RENDER_PLUGIN_INCLUDES) -o $@ -c $<


objs/renderinfo/%.o: %.cpp
	@echo " Building object $@"
	@mkdir -p `dirname $@`
	$(CXX) $(CXXFLAGS) $(RENDER_INFO_INCLUDES) -o $@ -c $<

	
clean:
	@echo Cleaning
	@rm -rf out objs
	@rm -f `find . -name "*.pyc"`
